package kr.saeildamil.androidstudy_second33;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class ChangeVolume extends Activity {
	MediaPlayer mPlayer;
	AudioManager mAudioManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_changevolume);
		mPlayer=new MediaPlayer();
		String sd=Environment.getExternalStorageDirectory().getAbsolutePath();
		try {
			mPlayer.setDataSource(sd+"/Music/The Eagles-Desparado.mp3");
			mPlayer.prepare();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mAudioManager=(AudioManager) getSystemService(Context.AUDIO_SERVICE);
		SeekBar seekBar=(SeekBar) findViewById(R.id.seekvolume);
		seekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		seekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
		seekBar.setOnSeekBarChangeListener(mOnSeek);
	}

	OnSeekBarChangeListener mOnSeek=new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
		}
	};
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mPlayer!=null){
			mPlayer.release();
			mPlayer=null;
		}
	}

	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.btnplay:
			if(mPlayer.isPlaying()){
				mPlayer.pause();
			}else{
				mPlayer.start();
			}
			break;
		case R.id.btnvolup:
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
			AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI |
			AudioManager.FLAG_ALLOW_RINGER_MODES |
			AudioManager.FLAG_PLAY_SOUND |
			AudioManager.FLAG_VIBRATE);
			break;
			
		case R.id.btnvoldown:
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
					AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI |
					AudioManager.FLAG_ALLOW_RINGER_MODES |
					AudioManager.FLAG_PLAY_SOUND |
					AudioManager.FLAG_VIBRATE);
			break;
		case R.id.btnring:
			switch (mAudioManager.getRingerMode()) {
			case AudioManager.RINGER_MODE_NORMAL:
				mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				Toast.makeText(this, "무음 모드", 0).show();
				break;

			case AudioManager.RINGER_MODE_SILENT:
				mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				Toast.makeText(this, "진동 모드", 0).show();
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				Toast.makeText(this, "일반 모드", 0).show();
			}
			break;
		case R.id.btneffect1:
			mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
			break;
		case R.id.btneffect2:
			mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
			break;
		case R.id.btneffect3:
			mAudioManager.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
			break;
		}
	}
}
